// pages/welcome/welcome.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      player: [
        {
          id: '001',
          name: '达怪',
          score: 0,
          current: 0,
          color: '#fc0'
        },
        {
          id: '002',
          name: '颜老怪',
          score: 0,
          current: 0,
          color: '#57db6a'
        },
        {
          id: '003',
          name: '蛋怪',
          score: 0,
          current: 0,
          color: '#3dbdee'
        },
        {
          id:'004',
          name: '毛怪',
          score: 0,
          current: 0,
          color: '#e60021'
        }
      ],
      selectedPlayer:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    selectPlayer(e){
      let selectedPlayer = app.globalData.selectedPlayer

      const item = e.target.dataset.item;
      const hasExist = selectedPlayer.find(val => val.name === item.name)
      if(hasExist) {
        selectedPlayer = selectedPlayer.filter(val => val.name !== item.name)
      }else{
        if(selectedPlayer.length >= 3) {
          wx.showToast({
            title: '不得超过3个人',
            icon: 'none'
          })
          return
        }
        
        selectedPlayer = [item, ...selectedPlayer]
      }
      
      this.setData({ selectedPlayer })
      
      app.globalData.selectedPlayer = selectedPlayer
    },
    start1(e){
      if(this.data.selectedPlayer.length != 3) {
        wx.showToast({
          title: `玩家必须是3个人`,
          icon: 'none'
        })
        return
      }
      wx.navigateTo({
        url: '/pages/index/index'
      })
      if(e) wx.clearStorage()
    },
    start2(e) {
      if(this.data.selectedPlayer.length != 2) {
        wx.showToast({
          title: `玩家必须2个人`,
          icon: 'none'
        })
        return
      }
      wx.navigateTo({
        url: '/pages/510k/index',
      })
      if(e) wx.clearStorage()
    },
    remove(e){
      const item = e.target.dataset.item;
      const selectedPlayer = this.data.selectedPlayer.filter(val => val.id !== item.id)
      this.setData({
        selectedPlayer
      })
      app.globalData.selectedPlayer = selectedPlayer
    },
    continueGame() {
      const landlord = wx.getStorageSync('landlord')
      const _510k = wx.getStorageSync('_510k')
      if(landlord?.selectedPlayer) {
        this.data.selectedPlayer = landlord.selectedPlayer
        this.start1()
        return;
      }

      if(_510k?.selectedPlayer) {
        this.data.selectedPlayer = _510k.selectedPlayer
        this.start2()
        return;
      }
      wx.showToast({
        title: '没有上一把数据',
        icon: 'none'
    })
    }
})